<?php

namespace App\Http\Controllers\Api;

use App\Exports\ExternalErrorReportExport;
use App\Exports\OrderExport;
use App\Exports\TestExport;
use App\Http\Controllers\Controller;
use App\Rep\OrderRep;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class Order extends Controller
{
    //订单管理
    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new OrderRep();
    }

    /**
     * 获取订单列表
     * @param Request $request
     */
    public function getOrderList(Request $request){
        $params = $request->all();
        $export = $request->get('export')??0;
        if($export == 1){
            try{
                $params['order_name'] = '订单列表';
                $filename = date('ymdhis').'.xlsx';
                $export = new OrderExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }
        $data = $this->rep->getOrderListRep($params);
        return re(0,$data,'success');
    }

    /**
     * 添加订单
     * @param Request $request
     */
    public function createOrder(Request $request){
        //$params = $request->all();
        $order_data = $request->input('order_data');
        $user_id = $request->input('user_id');
        $username = $request->input('username');
        $role_id = $request->input('role_id');
        $role_name = $request->input('role_name');
        $params = ['order_data'=>$order_data,'user_id'=>$user_id,'username'=>$username,'role_id'=>$role_id,'role_name'=>$role_name];
        $data = $this->rep->createOrderRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 修改订单
     */
    public function alertOrder(Request $request){
        $order_data = $request->input('order_data');
        $user_id = $request->input('user_id');
        $username = $request->input('username');
        $role_id = $request->input('role_id');
        $role_name = $request->input('role_name');
        $params = ['order_data'=>$order_data,'user_id'=>$user_id,'username'=>$username,'role_id'=>$role_id,'role_name'=>$role_name];
        $data = $this->rep->alertOrdersRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 获取审核订单列表
     * @param Request $request
     */
    public function getAuditOrderList(Request $request){
        $params = $request->all();
        $export = $request->get('export')??0;
        if($export == 1){
            try{
                $params['order_name'] = '订单审核';
                $filename = date('ymdhis').'.xlsx';
                $export = new OrderExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }
        $data = $this->rep->getAuditOrderListRep($params);
        return re(0,$data,'success');
    }


    /**
     * 审核订单
     */
    public function auditOrder(Request $request){
        $id = $request->input('id');
        $field = $request->input('field');
        $status = $request->input('status');
        $remark = $request->input('remark');
        $is_kucun = $request->input('is_kucun');
        $data = $this->rep->auditOrderRep($id,$field,$status,$remark,$is_kucun);
        return re(0,$data);
    }

    /**
     * 删除订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delOrder(Request $request){
        $id = $request->only(['id']);
        $data = $this->rep->delOrderRep($id);
        return re($data['code']);
    }


    /**
     * 获取审核订单流程
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOrderAuditFlow(Request $request){
        $data = $this->rep->getOrderAuditFlowRep();
        return re(0,$data,'success');
    }

    //自动生成流水号
    public function automaticGenerationSerialNumber(){
        $str = $this->rep->automaticGenerationSerialNumberRep();
        return re(0,$str,'success');
    }

    //自动生成订单号
    public function automaticGenerationOrderNumber(){
        $str = $this->rep->automaticGenerationOrderNumberRep();
        return re(0,$str,'success');
    }






}
